<?php

namespace app\api\model;

use think\Model;

class Vacate extends Model
{

    public static function doVacate($userId, $scheduleId,$reason)
    {
        $data["user_id"] = $userId;
        $data["schedule_id"] = $scheduleId;
        $data["status"] = 1;
        $data["reason"] = $reason;
        return self::create($data);
    }

    public static  function queryAllVacateList($groupid,$userid=0){
        //系统管理或教学管理  全查
        //教师    只看到自己的
        //学生    只看到自己的
        $data["v.status"] = 1;
        if($groupid == 10004 || $groupid == 10003){
            //管理
        }else if($groupid == 10002){//教师
            $data["ut.user_id"] = $userid;
        }else{//学生
            $data["us.user_id"] = $userid;
        }
        $Vacate = new Vacate();
        $info = $Vacate->alias('v')->where($data)
            ->join('stu_schedule s','s.schedule_id = v.schedule_id','left')//具体课程表
            ->join('stu_course c','s.course_id = c.course_id and s.class_id = c.class_id','left')//课程名表
            ->join('stu_user ut','c.user_id = ut.user_id','left')//用户表(教师信息)
            ->join('stu_user us','v.user_id = us.user_id','left')//用户表(学生信息)
            ->join('stu_class cs','s.class_id = cs.class_id','left')//班级表
            ->field('v.*,c.course_name,ut.username as teachername,us.username as studentname,cs.class_name')
            ->select();

        return $info;
    }

    public static function queryVacateById($vacateid)
    {
        $data["v.vacate_id"] = $vacateid;
        $Vacate = new Vacate();
        $info = $Vacate->alias('v')->where($data)
            ->join('stu_schedule s','s.schedule_id = v.schedule_id','left')//具体课程表
            ->join('stu_course c','s.course_id = c.course_id and s.class_id = c.class_id','left')//课程名表
            ->join('stu_user ut','c.user_id = ut.user_id','left')//用户表(教师信息)
            ->join('stu_user us','v.user_id = us.user_id','left')//用户表(学生信息)
            ->join('stu_class cs','s.class_id = cs.class_id','left')//班级表
            ->field('v.*,c.course_name,ut.username as teachername,us.username as studentname,cs.class_name')
            ->find();

        return $info;
    }


    public static function auditVacate($vacateid,$status, $teacherOpinion)
    {
        $data["teacher_opinion"] = $teacherOpinion;
        $data["status"] = $status;
        $where["vacate_id"] = $vacateid;
        return self::where($where)->update($data);
    }
}